package com.solidtrust.admin.controller;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solidtrust.admin.service.PriceService;
import com.solidtrust.app.SolidtrustConstants;
import com.solidtrust.entity.Area;
import com.solidtrust.entity.Build;
import com.solidtrust.entity.Ganji;
import com.solidtrust.entity.Volume;
import com.solidtrust.repository.AreaRepository;
import com.solidtrust.repository.BrandRepository;
import com.solidtrust.repository.BuildRepository;
import com.solidtrust.repository.GanjiRepository;
import com.solidtrust.repository.ModelRepository;
import com.solidtrust.repository.VolumeRepository;

/*
 * Ganji
 * 
 */
@Controller
@RequestMapping("price2")
public class PriceController2 {
	
	@Autowired PriceService service;
	@Autowired GanjiRepository rep;
	@Autowired BrandRepository brandRep;
	@Autowired ModelRepository modelRep;
	@Autowired BuildRepository buildRep;
	@Autowired AreaRepository areaRep;
	@Autowired VolumeRepository volRep;

	@Transactional(readOnly = true)
	@RequestMapping("index")
	public String index(
			Model m,
			@RequestParam(value="page", required=false, defaultValue="1") Integer pageIndex, 
			@RequestParam(value="pageSize", required=false, defaultValue="20") Integer pageSize,
			@RequestParam(value="area", required=false) Integer area,
			@RequestParam(value="brand", required=false) Integer brand,
			@RequestParam(value="model", required=false) Integer model,
			@RequestParam(value="build", required=false) Integer build,
			@RequestParam(value="volume", required=false) Integer volume,
			@RequestParam(value="mileage", required=false) Integer mileage){
		
		if(area != null){
			Pageable pageable = new PageRequest(pageIndex - 1, pageSize);
			Date from = DateUtils.addMonths(new Date(), -4);
			
			//mileage 
			int start, end;
			if(mileage == 1){
				start = 1; //exclude mileage = 0
			}else{
				start = SolidtrustConstants.MILEAGE_RANGE[mileage - 2];
			}
			if(mileage == SolidtrustConstants.MILEAGE_RANGE.length + 1){
				end = 9999999;
			}else{
				end = SolidtrustConstants.MILEAGE_RANGE[mileage - 1];
			}
			
			Volume v = volRep.findOne(volume);
			Build b = v.getBuild();
			Area c = areaRep.findOne(area);
			
			Page<Ganji> p = rep.findByCityAndBrandAndModelAndBuildAndVolumeAndScrapedDateGreaterThanAndMileageBetween(pageable, 
					c.getNameOfGanji(), 
					b.getModel().getBrand().getBrand(), 
					b.getModel().getModel(), 
					b.getBuild(),
					v.getVolume(),
					from, start, end);
			Object[] aggregate = rep.aggregate(
					c.getNameOfGanji(), 
					b.getModel().getBrand().getBrand(), 
					b.getModel().getModel(), 
					b.getBuild(), 
					v.getVolume(), 
					from, start, end);
			
			m.addAttribute("page", p);
			m.addAttribute("aggregate", aggregate);
			m.addAttribute("area", area);
			m.addAttribute("brand", brand);
			m.addAttribute("model", model);
			m.addAttribute("build", build);
			m.addAttribute("volume", volume);
			m.addAttribute("mileage", mileage);
			
		}
		return "admin/price/index2.jsp";
	}
	
	@Transactional(readOnly = true)
	@ResponseBody
	@RequestMapping("getCategories")
	public List<?> getCategories(Integer level, Integer parent){
		List<?> list = null;
		if(level == 0){
			list = brandRep.findByStatus((short)1);
		}
		
		else if(level == 1){
			list = brandRep.findOne(parent).getModels();
		}
		
		else if(level == 2){
			list = modelRep.findOne(parent).getBuilds();
		}
		
		else if(level == 3){
			List<Volume> volumes = buildRep.findOne(parent).getVolumes();
			list = volumes.stream().filter(p -> p.getCount() >= 5).collect(Collectors.toList());
		}
		
		if(list != null){
			list.size();
		}
		
		return list;
	}
	
}
